home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 2.iso / programr / eckelt01.zip / 17 / RTSHAPES.CPP < prev    next >
C/C++ Source or Header  |  1995-02-23  |  4KB  |  141 lines

  1. // File from page 725 in "Thinking in C++" by Bruce Eckel
  2. //////////////////////////////////////////////////
  3. // From the compressed package ECKELT01.ZIP 2/21/95
  4. // Copyright (c) Bruce Eckel, 1995 
  5. // Source code file from the book "Thinking in C++", 
  6. // Prentice Hall, 1995, ISBN: 0-13-917709-4
  7. // All rights reserved EXCEPT as allowed by the following 
  8. // statements: You may freely use this file for your own 
  9. // work, including modifications and distribution in 
  10. // executable form only. You may copy and distribute this 
  11. // file, as long as it is only distributed in the complete 
  12. // (compressed) package with the other files from this 
  13. // book and you do not remove this copyright and notice. 
  14. // You may not distribute modified versions of the source 
  15. // code in this package. This package may be freely placed 
  16. // on bulletin boards, internet nodes, shareware disks and 
  17. // product vendor disks. You may not use this file in 
  18. // printed media without the express permission of the 
  19. // author. Bruce Eckel makes no 
  20. // representation about the suitability of this software 
  21. // for any purpose. It is provided "as is" without express 
  22. // or implied warranty of any kind. The entire risk as to 
  23. // the quality and performance of the software is with 
  24. // you. Should the software prove defective, you assume 
  25. // the cost of all necessary servicing, repair, or 
  26. // correction. 
  27. // If you think you've found an error, please 
  28. // email all modified files with loudly commented changes 
  29. // to: eckel@aol.com (please use the same 
  30. // address for non-code errors found in the book).
  31. //////////////////////////////////////////////////
  32.  
  33. //: RTSHAPES.CPP -- Counting shapes
  34. #include <iostream.h>
  35. #include <time.h>
  36. #include <typeinfo.h>
  37. #include "..\14\tstash.h"
  38.  
  39. class shape {
  40. protected:
  41.   static int count;
  42. public:
  43.   shape() { count++; }
  44.   virtual ~shape() = 0 { count--; }
  45.   virtual void draw() const = 0;
  46.   static int quantity() { return count; }
  47. };
  48.  
  49. int shape::count = 0;
  50.  
  51. class rectangle : public shape {
  52.   void operator=(rectangle&); // Disallow
  53. protected:
  54.   static int count;
  55. public:
  56.   rectangle() { count++; }
  57.   rectangle(const rectangle&) { count++;}
  58.   ~rectangle() { count--; }
  59.   void draw() const {
  60.     cout << "rectangle::draw()" << endl;
  61.   }
  62.   static int quantity() { return count; }
  63. };
  64.  
  65. int rectangle::count = 0;
  66.  
  67. class ellipse : public shape {
  68.   void operator=(ellipse&); // Disallow
  69. protected:
  70.   static int count;
  71. public:
  72.   ellipse() { count++; }
  73.   ellipse(const ellipse&) { count++; }
  74.   ~ellipse() { count--; }
  75.   void draw() const {
  76.     cout << "ellipse::draw()" << endl;
  77.   }
  78.   static int quantity() { return count; }
  79. };
  80.  
  81. int ellipse::count = 0;
  82.  
  83. class circle : public ellipse {
  84.   void operator=(circle&); // Disallow
  85. protected:
  86.   static int count;
  87. public:
  88.   circle() { count++; }
  89.   circle(const circle&) { count++; }
  90.   ~circle() { count--; }
  91.   void draw() const {
  92.     cout << "circle::draw()" << endl;
  93.   }
  94.   static int quantity() { return count; }
  95. };
  96.  
  97. int circle::count = 0;
  98.  
  99. main() {
  100.   tstash<shape> shapes;
  101.   time_t t;
  102.   // Seed random number generator:
  103.   srand((unsigned)time(&t));
  104.   const mod = 12;
  105.   for(int i = 0; i < rand() % mod; i++)
  106.     shapes.add(new rectangle);
  107.   for(i = 0; i < rand() % mod; i++)
  108.     shapes.add(new ellipse);
  109.   for(i = 0; i < rand() % mod; i++)
  110.     shapes.add(new circle);
  111.   int Ncircles = 0;
  112.   int Nellipses = 0;
  113.   int Nrects = 0;
  114.   int Nshapes = 0;
  115.   for(i = 0; i < shapes.count(); i++) {
  116.     shapes[i]->draw();
  117.     if(dynamic_cast<circle*>(shapes[i]))
  118.       Ncircles++;
  119.     if(dynamic_cast<ellipse*>(shapes[i]))
  120.       Nellipses++;
  121.     if(dynamic_cast<rectangle*>(shapes[i]))
  122.       Nrects++;
  123.     if(dynamic_cast<shape*>(shapes[i]))
  124.       Nshapes++;
  125.   }
  126.   cout << endl << endl
  127.        << "circles = " << Ncircles << endl
  128.        << "ellipses = " << Nellipses << endl
  129.        << "rectangles = " << Nrects << endl
  130.        << "shapes = " << Nshapes << endl
  131.        << endl
  132.        << "circle::quantity() = "
  133.        << circle::quantity() << endl
  134.        << "ellipse::quantity() = "
  135.        << ellipse::quantity() << endl
  136.        << "rectangle::quantity() = "
  137.        << rectangle::quantity() << endl
  138.        << "shape::quantity() = "
  139.        << shape::quantity() << endl;
  140. }
  141.